﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using MVC4App.Repositories.Core;
using MVC4App.Repositories.Core.Security;
using MVC4App.SecurityServiceReference;

namespace MVC4App.Repositories.Implementations.Security
{
    public class SiteRepository : RepositoryBase, ISiteRepository
    {
        public List<SiteDto> GetList(int userId, int parentId)
        {
            var request = new SiteRequest().Prepare();
            request.LoadOptions = new[] { "SitesByUser" };
            request.Criteria = new SiteCriteria() { UserId = userId, ParentId = parentId };
            var response = SecurityServiceClient.GetSites(request);
            Correlate(request, response);
            return response.Sites == null ? null : response.Sites.ToList();
        }

        public List<SiteDto> GetListByParentId(int parentId)
        {
            var request = new SiteRequest().Prepare();
            request.LoadOptions = new[] { "SitesByParentId" };
            request.Criteria = new SiteCriteria() {ParentId = parentId };
            var response = SecurityServiceClient.GetSites(request);
            Correlate(request, response);
            return response.Sites == null ? null : response.Sites.ToList();
        }

        public List<SiteDto> GetAdvanced(string keyword, bool? status,string sort, ref int totalPage, int currentPage = 1)
        {
            var request = new SiteRequest().Prepare();
            request.LoadOptions = new[] { "Search" };
            request.CurrentPage = currentPage;
            request.TotalPage = totalPage;
            request.Criteria=new SiteCriteria(){Keyword = keyword,Status = status,SortExpression = sort};
            var response = SecurityServiceClient.GetSites(request);
            Correlate(request, response);
            totalPage = response.TotalPage;
            return response.Sites == null ? null : response.Sites.ToList();
        }                

        public List<SiteDto> GetList(Criterion criterion = null)
        {
            throw new NotImplementedException();
        }

        public List<SiteDto> GetQuickSearchPaged(ref int totalPage, int currentPage=1, string keyword = null)
        {
            var request = new SiteRequest().Prepare();
            request.LoadOptions = new[] { "QuickSearch" };
            request.CurrentPage = currentPage;
            request.TotalPage = totalPage;
            request.Site=new SiteDto(){Title = keyword};
            var response = SecurityServiceClient.GetSites(request);
            Correlate(request, response);
            totalPage = response.TotalPage;
            return response.Sites == null ? null : response.Sites.ToList();
        }

        public List<SiteDto> GetPaged(ref int totalPage, int currentPage = 1)
        {
            var request = new SiteRequest().Prepare();
            request.LoadOptions = new[] { "Sites" };
            request.OptionTypes=OptionType.IsPaging;
            request.CurrentPage = currentPage;
            request.TotalPage = totalPage;
            var response = SecurityServiceClient.GetSites(request);
            Correlate(request, response);
            totalPage = response.TotalPage;
            return response.Sites == null ? null : response.Sites.ToList();
        }

        public SiteDto Get(int id)
        {
            var request = new SiteRequest().Prepare();
            request.LoadOptions = new[] { "Site" };
            request.Criteria = new SiteCriteria() { Id = id };
            var response = SecurityServiceClient.GetSites(request);
            Correlate(request, response);
            return response.Site;
        }        

        public string Insert(SiteDto t)
        {
            throw new NotImplementedException();
        }

        public string Update(SiteDto t)
        {
            throw new NotImplementedException();
        }

        public string Delete(int id)
        {
            throw new NotImplementedException();
        }
    }
}